1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package java.awt.image;
28
29 import java.awt.color.ColorSpace;
30 import java.awt.geom.Rectangle2D;
31 import java.awt.Rectangle;
32 import java.awt.RenderingHints;
33 import java.awt.geom.Point2D;
34 import sun.awt.image.ImagingLib;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public class LookupOp implements BufferedImageOp, RasterOp {
78 private LookupTable ltable;
79 private int numComponents;
80 RenderingHints hints;
81
82
83
84
85
86
87
88
89
90 public LookupOp(LookupTable lookup, RenderingHints hints) {
91 this.ltable = lookup;
92 this.hints = hints;
93 numComponents = ltable.getNumComponents();
94 }
95
96
97
98
99
100
101 public final LookupTable getTable() {
102 return ltable;
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public final BufferedImage filter(BufferedImage src, BufferedImage dst) {
126 ColorModel srcCM = src.getColorModel();
127 int numBands = srcCM.getNumColorComponents();
128 ColorModel dstCM;
129 if (srcCM instanceof IndexColorModel) {
130 throw new
131 IllegalArgumentException("LookupOp cannot be "+
132 "performed on an indexed image");
133 }
134 int numComponents = ltable.getNumComponents();
135 if (numComponents != 1 &&
136 numComponents != srcCM.getNumComponents() &&
137 numComponents != srcCM.getNumColorComponents())
138 {
139 throw new IllegalArgumentException("Number of arrays in the "+
140 " lookup table ("+
141 numComponents+
142 " is not compatible with the "+
143 " src image: "+src);
144 }
145
146
147 boolean needToConvert = false;
148
149 int width = src.getWidth();
150 int height = src.getHeight();
151
152 if (dst == null) {
153 dst = createCompatibleDestImage(src, null);
154 dstCM = srcCM;
155 }
156 else {
157 if (width != dst.getWidth()) {
158 throw new
159 IllegalArgumentException("Src width ("+width+
160 ") not equal to dst width ("+
161 dst.getWidth()+")");
162 }
163 if (height != dst.getHeight()) {
164 throw new
165 IllegalArgumentException("Src height ("+height+
166 ") not equal to dst height ("+
167 dst.getHeight()+")");
168 }
169
170 dstCM = dst.getColorModel();
171 if (srcCM.getColorSpace().getType() !=
172 dstCM.getColorSpace().getType())
173 {
174 needToConvert = true;
175 dst = createCompatibleDestImage(src, null);
176 }
177
178 }
179
180 BufferedImage origDst = dst;
181
182 if (ImagingLib.filter(this, src, dst) == null) {
183
184 WritableRaster srcRaster = src.getRaster();
185 WritableRaster dstRaster = dst.getRaster();
186
187 if (srcCM.hasAlpha()) {
188 if (numBands-1 == numComponents || numComponents == 1) {
189 int minx = srcRaster.getMinX();
190 int miny = srcRaster.getMinY();
191 int[] bands = new int[numBands-1];
192 for (int i=0; i < numBands-1; i++) {
193 bands[i] = i;
194 }
195 srcRaster =
196 srcRaster.createWritableChild(minx, miny,
197 srcRaster.getWidth(),
198 srcRaster.getHeight(),
199 minx, miny,
200 bands);
201 }
202 }
203 if (dstCM.hasAlpha()) {
204 int dstNumBands = dstRaster.getNumBands();
205 if (dstNumBands-1 == numComponents || numComponents == 1) {
206 int minx = dstRaster.getMinX();
207 int miny = dstRaster.getMinY();
208 int[] bands = new int[numBands-1];
209 for (int i=0; i < numBands-1; i++) {
210 bands[i] = i;
211 }
212 dstRaster =
213 dstRaster.createWritableChild(minx, miny,
214 dstRaster.getWidth(),
215 dstRaster.getHeight(),
216 minx, miny,
217 bands);
218 }
219 }
220
221 filter(srcRaster, dstRaster);
222 }
223
224 if (needToConvert) {
225
226 ColorConvertOp ccop = new ColorConvertOp(hints);
227 ccop.filter(dst, origDst);
228 }
229
230 return origDst;
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 public final WritableRaster filter (Raster src, WritableRaster dst) {
254 int numBands = src.getNumBands();
255 int dstLength = dst.getNumBands();
256 int height = src.getHeight();
257 int width = src.getWidth();
258 int srcPix[] = new int[numBands];
259
260
261
262 if (dst == null) {
263 dst = createCompatibleDestRaster(src);
264 }
265 else if (height != dst.getHeight() || width != dst.getWidth()) {
266 throw new
267 IllegalArgumentException ("Width or height of Rasters do not "+
268 "match");
269 }
270 dstLength = dst.getNumBands();
271
272 if (numBands != dstLength) {
273 throw new
274 IllegalArgumentException ("Number of channels in the src ("
275 + numBands +
276 ") does not match number of channels"
277 + " in the destination ("
278 + dstLength + ")");
279 }
280 int numComponents = ltable.getNumComponents();
281 if (numComponents != 1 && numComponents != src.getNumBands()) {
282 throw new IllegalArgumentException("Number of arrays in the "+
283 " lookup table ("+
284 numComponents+
285 " is not compatible with the "+
286 " src Raster: "+src);
287 }
288
289
290 if (ImagingLib.filter(this, src, dst) != null) {
291 return dst;
292 }
293
294
295 if (ltable instanceof ByteLookupTable) {
296 byteFilter ((ByteLookupTable) ltable, src, dst,
297 width, height, numBands);
298 }
299 else if (ltable instanceof ShortLookupTable) {
300 shortFilter ((ShortLookupTable) ltable, src, dst, width,
301 height, numBands);
302 }
303 else {
304
305 int sminX = src.getMinX();
306 int sY = src.getMinY();
307 int dminX = dst.getMinX();
308 int dY = dst.getMinY();
309 for (int y=0; y < height; y++, sY++, dY++) {
310 int sX = sminX;
311 int dX = dminX;
312 for (int x=0; x < width; x++, sX++, dX++) {
313
314 src.getPixel(sX, sY, srcPix);
315
316
317 ltable.lookupPixel(srcPix, srcPix);
318
319
320 dst.setPixel(dX, dY, srcPix);
321 }
322 }
323 }
324
325 return dst;
326 }
327
328
329
330
331
332
333
334
335 public final Rectangle2D getBounds2D (BufferedImage src) {
336 return getBounds2D(src.getRaster());
337 }
338
339
340
341
342
343
344
345
346 public final Rectangle2D getBounds2D (Raster src) {
347 return src.getBounds();
348
349 }
350
351
352
353
354
355
356
357
358
359
360 public BufferedImage createCompatibleDestImage (BufferedImage src,
361 ColorModel destCM) {
362 BufferedImage image;
363 int w = src.getWidth();
364 int h = src.getHeight();
365 int transferType = DataBuffer.TYPE_BYTE;
366 if (destCM == null) {
367 ColorModel cm = src.getColorModel();
368 Raster raster = src.getRaster();
369 if (cm instanceof ComponentColorModel) {
370 DataBuffer db = raster.getDataBuffer();
371 boolean hasAlpha = cm.hasAlpha();
372 boolean isPre = cm.isAlphaPremultiplied();
373 int trans = cm.getTransparency();
374 int[] nbits = null;
375 if (ltable instanceof ByteLookupTable) {
376 if (db.getDataType() == db.TYPE_USHORT) {
377
378 if (hasAlpha) {
379 nbits = new int[2];
380 if (trans == cm.BITMASK) {
381 nbits[1] = 1;
382 }
383 else {
384 nbits[1] = 8;
385 }
386 }
387 else {
388 nbits = new int[1];
389 }
390 nbits[0] = 8;
391 }
392
393 }
394 else if (ltable instanceof ShortLookupTable) {
395 transferType = DataBuffer.TYPE_USHORT;
396 if (db.getDataType() == db.TYPE_BYTE) {
397 if (hasAlpha) {
398 nbits = new int[2];
399 if (trans == cm.BITMASK) {
400 nbits[1] = 1;
401 }
402 else {
403 nbits[1] = 16;
404 }
405 }
406 else {
407 nbits = new int[1];
408 }
409 nbits[0] = 16;
410 }
411 }
412 if (nbits != null) {
413 cm = new ComponentColorModel(cm.getColorSpace(),
414 nbits, hasAlpha, isPre,
415 trans, transferType);
416 }
417 }
418 image = new BufferedImage(cm,
419 cm.createCompatibleWritableRaster(w, h),
420 cm.isAlphaPremultiplied(),
421 null);
422 }
423 else {
424 image = new BufferedImage(destCM,
425 destCM.createCompatibleWritableRaster(w,
426 h),
427 destCM.isAlphaPremultiplied(),
428 null);
429 }
430
431 return image;
432 }
433
434
435
436
437
438
439
440 public WritableRaster createCompatibleDestRaster (Raster src) {
441 return src.createCompatibleWritableRaster();
442 }
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457 public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) {
458 if (dstPt == null) {
459 dstPt = new Point2D.Float();
460 }
461 dstPt.setLocation(srcPt.getX(), srcPt.getY());
462
463 return dstPt;
464 }
465
466
467
468
469
470
471 public final RenderingHints getRenderingHints() {
472 return hints;
473 }
474
475 private final void byteFilter(ByteLookupTable lookup, Raster src,
476 WritableRaster dst,
477 int width, int height, int numBands) {
478 int[] srcPix = null;
479
480
481 byte[][] table = lookup.getTable();
482 int offset = lookup.getOffset();
483 int tidx;
484 int step=1;
485
486
487 if (table.length == 1) {
488 step=0;
489 }
490
491 int x;
492 int y;
493 int band;
494 int len = table[0].length;
495
496
497 for ( y=0; y < height; y++) {
498 tidx = 0;
499 for ( band=0; band < numBands; band++, tidx+=step) {
500
501 srcPix = src.getSamples(0, y, width, 1, band, srcPix);
502
503 for ( x=0; x < width; x++) {
504 int index = srcPix[x]-offset;
505 if (index < 0 || index > len) {
506 throw new
507 IllegalArgumentException("index ("+index+
508 "(out of range: "+
509 " srcPix["+x+
510 "]="+ srcPix[x]+
511 " offset="+ offset);
512 }
513
514 srcPix[x] = table[tidx][index];
515 }
516
517 dst.setSamples(0, y, width, 1, band, srcPix);
518 }
519 }
520 }
521
522 private final void shortFilter(ShortLookupTable lookup, Raster src,
523 WritableRaster dst,
524 int width, int height, int numBands) {
525 int band;
526 int[] srcPix = null;
527
528
529 short[][] table = lookup.getTable();
530 int offset = lookup.getOffset();
531 int tidx;
532 int step=1;
533
534
535 if (table.length == 1) {
536 step=0;
537 }
538
539 int x = 0;
540 int y = 0;
541 int index;
542 int maxShort = (1<<16)-1;
543
544 for (y=0; y < height; y++) {
545 tidx = 0;
546 for ( band=0; band < numBands; band++, tidx+=step) {
547
548 srcPix = src.getSamples(0, y, width, 1, band, srcPix);
549
550 for ( x=0; x < width; x++) {
551 index = srcPix[x]-offset;
552 if (index < 0 || index > maxShort) {
553 throw new
554 IllegalArgumentException("index out of range "+
555 index+" x is "+x+
556 "srcPix[x]="+srcPix[x]
557 +" offset="+ offset);
558 }
559
560 srcPix[x] = table[tidx][index];
561 }
562
563 dst.setSamples(0, y, width, 1, band, srcPix);
564 }
565 }
566 }
567 }